home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-21 | 8.9 KB | 405 lines | [TEXT/ttxt] |
- -- Filename:
- -- animdemo.sx
-
- -- Other Files Required:
- -- This file is loaded by loadme.sx in the animate folder.
-
- -- Purpose:
- -- animdemo.sx defines a class AnimationDemo, a subclass of Window, which creates
- -- an instance of Animation and adds to the window.
-
- -- Specialized Classes:
- -- AnimationDemo
-
- -- Instructions to User:
- -- The ScriptX code "show (new AnimationDemo)" creates the window, adds an
- -- instance of class Animation, and shows the window.
-
- -- Author:
- -- Steve Mayer
-
- in module InternetFish
-
- --- Edit these to set the defaults
-
- global defaultPort := "2000"
- global defaultServer := "pccer"
-
- class fishui ()
- instance variables
- win : (new TwoDSpace boundary: (new rect x2: 200 y2: 160))
- portField : (new smalltextedit text: defaultPort)
- portLabel : (new label text: "Port:")
- serverField : (new smalltextedit text: defaultServer)
- serverLabel : (new label text: "Server:")
- okButton : (new textbutton text: "ok")
- client
- getgoing
- end
-
- function myappend x y -> (
- y.stationary := false
- append x y
- )
-
- method init self {object fishui} #rest args #key client: getgoing: showServer: (false) -> (
- apply nextmethod self args
-
- self.client := client
- self.getgoing := getgoing
-
- myappend self.win self.portField
- myappend self.win self.portLabel
-
- if showServer do (
- myappend self.win self.serverLabel
- myappend self.win self.serverField
- )
-
- myappend self.win self.okButton
-
- local bc := new actuatorcontroller space: self.win
- bc.wholespace := true
- --
-
- self.portLabel.x := 10
- self.portLabel.y := 80
-
- self.portField.x := 70
- self.portField.y := 80
-
- self.serverLabel.x := 10
- self.serverLabel.y := 50
-
- self.serverField.x := 70
- self.serverField.y := 50
- self.serverField.width := 120
-
- self.okButton.x := 80
- self.okButton.y := 110
-
- self.okButton.activateAction := (ignore1 ignore2 -> okButtonPressed self)
-
-
- self.win.x := (self.client.width - self.win.width) / 2
- self.win.y := (self.client.height - self.win.height) / 2
- prepend self.client self.win
- )
-
- method okButtonPressed self {object fishui} -> (
- local server := self.serverField.text
- local port := self.portField.text as integer
- local serverPort := port
-
- defaultServer := self.serverField.text
- defaultPort := self.portField.text
-
- deleteOne self.client self.win
-
- self.getgoing self.client server: server serverPort: port port: port &
- )
-
- class AnimationDemo (Window)
-
- instance variables
- animal
- mc
- mw
- tcpListen
- state
- quitButton
- disconnectButton
- createButton
- connectButton
-
- currentConnection
- myCM
-
- end
-
- ---------------------------------------------------------------------------
- ---------------------------------------------------------------------------------------------------------
-
-
-
- class method startDemo self {class AnimationDemo} -> (
- local x := new self
- show x
- local ui := new fishui client: x getgoing: getgoing showserver: false
- x
- )
-
- method init self {class AnimationDemo} -> (
- nextMethod self boundary:(new Rect x2:500 y2:300) \
- name: "Internet Fish" \
- fill: (new Brush color: (new RGBColor red:0 green:40 blue:100))
- self.x := 40
- self.y := 40
- self.clock.scale := 10
-
- self.mc := new Movement space:self
-
- self.quitButton := new TextButton text: "Quit"
- self.quitButton.x := 0
- self.quitButton.y := 0
- self.quitButton.activateAction := (ignore1 ignore2 -> userQuit self)
-
- append self self.quitButton
-
- self.disconnectButton := new TextButton text: "Disconnect"
- self.disconnectButton.x := self.quitButton.width
- self.disconnectButton.y := 0
- self.disconnectButton.activateAction := (ignore1 ignore2 -> userDisconnect self)
-
- append self self.disconnectButton
-
- self.createButton := new TextButton text: "Create Fish"
- self.createButton.activateAction := (ignore1 ignore2 -> createFish self)
- self.createButton.x := self.disconnectButton.x + self.disconnectButton.width
-
- append self self.createButton
-
-
- self.connectButton := new TextButton text: "Connect"
- self.connectButton.activateAction := (ignore1 ignore2 -> connectCallback self)
- self.connectButton.x := self.createButton.x + self.createButton.width
-
- append self self.connectButton
-
- local bc := new actuatorcontroller space: self
- bc.wholespace := true
-
- setState self @ui
- )
-
- method getgoing self {object animationdemo} #key server: serverport: port: -> (
- /*
- guard (
- */
-
- local f := new Fish authorData:self
- f.x := self.width
- f.y := 70
-
- append self f
- append self.mc f
-
- self.mw := new Wrapper space:self client: self state: @standalone
- append self.mw f
- local d := new DragController space:self
- append d f
- self.animal := f
-
- self.myCM := new connectionManager client: self port: port
-
- return self
- )
-
- /*
- catching
- TCPException: ( caught (new fishui client: self getgoing: getgoing showserver: false)
- )
- end
- */
-
- /*
- )
- */
-
-
- -- Method prepareDrag is invoked when an object is grabbed. Remove
- -- the object from the movement controller.
- method prepareDrag self {class AnimationDemo} obj ->
- (
- deleteOne self.mc obj
- )
-
- -- Method drop is invoked when an object is dropped. Add the object
- -- to the movement controller.
- method drop self {class AnimationDemo} obj ->
- (
- if self.mw = @own and not (ismember self.mc obj) do
- appendnew self.mc obj
- )
-
- --- transitions
-
- method chooseCurrentConnection self {class AnimationDemo} -> (
- self.currentConnection := chooseConnection self.myCM
- )
-
- method loseCurrentConnection self {class AnimationDemo} -> (
- self.currentConnection := undefined
- )
-
- method ensureConnection self {object animationdemo} -> (
- if self.currentconnection != undefined then return true
- chooseCurrentConnection self
- if self.currentConnection = undefined then (
- self.mw.state := @standalone
- return false
- ) else (
- self.mw.state := @own
- return true
- )
- )
-
- method noteLosing self {object AnimationDemo} -> (
- if ensureConnection self do
- writePacket self @losing self.animal.x
- )
-
- method noteLost self {object AnimationDemo} -> (
- if ensureConnection self do (
- writePacket self @lost self.animal.x
- deleteOne self.mc self.animal
- loseCurrentConnection self
- )
- )
-
-
- method noteStillLosing self {object AnimationDemo} -> (
- if ensureConnection self do
- writePacket self @stilllosing self.animal.x
- )
-
- method noteGotback self {object AnimationDemo} -> (
- if ensureConnection self do (
- writePacket self @gotback 0
- loseCurrentConnection self
- )
- )
-
- method startSharing self {object AnimationDemo} -> (
- -- print "starting"
- self.animal.x := self.width
- self.mw.state := @gaining
- )
-
- method stillSharing self {object AnimationDemo} x -> (
- -- print "still"
- self.animal.x := x + self.width
- self.mw.state := @gaining
- )
-
- method gotback self {object AnimationDemo} -> (
- -- print "still"
- self.animal.x := self.width
- self.mw.state := @lost
- )
- method startOwning self {object AnimationDemo} -> (
- self.mw.state := @own
- resume self.clock
- if not (ismember self.mc self.animal) do
- append self.mc self.animal
- )
-
- method doQuit self {object AnimationDemo} -> (
- plug self.mqueue
- plug self.tcpStream
- )
-
- global packetstates := #(@lost, @losing, @stilllosing, @gotback, @quit)
-
- method writePacket self {object AnimationDemo} state x -> (
- local byte1
- local byte2
- local byte3
-
- byte1 := getordone packetstates state
- x := (-x) as integer
- byte2 := mod x 256
- byte3 := (x / 256) as integer
-
- write self.currentConnection.mQueue #(byte1, byte2, byte3)
- )
-
-
- method handleNormalMessage self {object animationdemo} obj -> (
- local packet := #(packetstates[obj[1]], -(obj[2] + (256 * obj[3])))
- case packet[1] of
- @lost : (
- startowning self
- return true
- )
- @losing : (
- startsharing self
- return false
- )
- @stilllosing : (
- stillSharing self packet[2]
- return false
- )
- @gotback : (
- gotback self
- return false
- )
- @quit : (
- handleQuitMessage self
- return true
- )
- end
- )
-
- method noteLostConnection self {object animationdemo} con -> (
- if self.currentConnection = con do (
- loseCurrentConnection self
- noteLosing self
- )
- )
-
- method noteNewConnection self {object animationdemo} con isFirst -> (
- if self.mw != undefined and self.mw.state = @standalone do
- self.mw.state := @own
- )
-
- method setState self {object animationdemo} state -> (
- self.state := state
- updateButtons self
- )
-
- method updateButtons self {object animationdemo} -> (
- self.quitButton.enabled := true
- self.disconnectButton.enabled := true
- )
-
-
- method handleQuitMessage self {object animationdemo} -> (
- -- Do nothing
- )
-
- method userQuit self {object animationdemo} -> (
- userDisconnect self
- stopListening self.myCM
- -- What else do you have to do?
- removeFish self
- hide self
- )
-
- method removeFish self {object animationdemo} -> (
- -- This is not quite right
- -- deleteOne self self.animal
- )
-
-
- method userDisconnect self {object animationdemo} -> (
- disconnect self.myCM
- )
-
- method createFish self {object animationdemo} -> (
- startowning self
- )
-
- method connectCallback self {object animationdemo} -> (
- new fishui client: self getgoing: doConnect showserver: true
- )
-
- method doConnect self {object animationdemo} #key state: server: serverport: port: -> (
- connectToServer self.myCM server port
- connectToSomeMore self.myCM
- )
-
-
-
- -- End of class definition for AnimationDemo.
-